using(['utils/fetchEnhancer', 'pc/js/utils/XForm', 'utils/helpers'], function(fetch, XForm, helpers){

var btn = document.getElementById('btn');
var f = new XForm();

f.checkOther('account', {
    minLength: 2,
    infoText : '请输入注册时的昵称或邮箱',
    errorText : '请输入注册时的昵称或邮箱'
});
f.checkOther('password', {
    minLength: 6,
    infoText : '请输入密码',
    errorText : '密码最少 6 位哦'
});
f.checkOther('verifycode', {
    minLength: 4,
    infoText : '请输入验证码',
    errorText : '最少 4 位哦'
});

btn.onclick = function(e) {
    var doc = document;
    var valid = f.validate();

    if(!valid) {
        return;
    }

    var account = doc.getElementById('account').value;
    var password = doc.getElementById('password').value;
    var verifyCode = doc.getElementById('verifycode').value;
    var isEmail = f.rulesConfig.email.regular.test(account) ? '1' : '0';

    btn.disabled = true;
    USER_SDK.getInstance().apiLogin({
        account: account,
        password: password,
        verifyCode: verifyCode,
        isEmail: isEmail
    }, function(data){
        btn.disabled = false;

        if(0 === data.status) {
            var ref = helpers.getQueryString('ref');
            if(ref) {
                window.location.href = ref;
            } else {
                window.location.href = helpers.U('ucenter');
            }

            return;
        }

        if(100004 === data.status) {
            f.setTipMessage('verifycode', '验证码错误');

        } else {
            f.setTipMessage('account', '用户名或密码错误');
        }
    });
};

});
